//
//  XMLoginViewController.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/8.
//

#import "XMLoginViewController.h"
#import "XMLoginView.h"
#import "XMLoginOtherPhoneController.h"
#import "XMNetwork+login.h"
#import "XMUserModel.h"
#import "XMBindPhoneViewController.h"
#import <WXApi.h>
#import <TencentOpenApi/TencentOAuth.h>
#import "XMServiceInfoViewController.h"

@interface XMLoginViewController ()<XMLoginViewDelegate, WXApiDelegate, TencentLoginDelegate, TencentSessionDelegate>

@property (nonatomic, strong) TencentOAuth *tencentOAth;

@property (nonatomic, strong) NSArray *permissions;

@end

@implementation XMLoginViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self hideNavigationBar:YES animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatUI];
}

- (void)creatUI {
    XMLoginView *loginView = [[XMLoginView alloc] initWithFrame:self.view.bounds];
    loginView.delegate = self;
    [self.view addSubview:loginView];
    _permissions = [NSArray arrayWithObjects:@"get_user_info",@"get_simple_userinfo", @"add_t", nil];
}

- (void)loginWithPhone:(NSString *)phoneNum {
    XMWeakSelf;
    [_Network loginWithPhoneNum:phoneNum
                        success:^(id  _Nonnull responseObject) {
        XMUserModel *userModel = [XMUserModel yy_modelWithJSON:[responseObject objectForKey:@"data"]];
        [[XMUserManager shareInstance] saveUserModel:userModel];
        weakSelf.loginSuccessBlock ? weakSelf.loginSuccessBlock() : nil;
        [weakSelf.navigationController dismissViewControllerAnimated:YES completion:nil];
    } failure:^(NSError * _Nonnull error) {

    }];
}

//其他手机号码登录
- (void)loginWithOtherPhone {
    XMLoginOtherPhoneController *loginOtherVC = [[XMLoginOtherPhoneController alloc] init];
    [self.navigationController pushViewController:loginOtherVC animated:YES];
}

- (void)loginWithQQ {
    [_tencentOAth authorize:_permissions inSafari:YES];
//    XMBindPhoneViewController *bindPhoneVC = [[XMBindPhoneViewController alloc] init];
//    [self.navigationController pushViewController:bindPhoneVC animated:YES];
}

//- (void)loginWithWechat {
//    SendAuthReq *req = [[SendAuthReq alloc] init];
//    req.scope = @"snsapi_userinfo";
//    req.state = @"8947";
//    XMWeakSelf;
//    [WXApi sendReq:req
//        completion:^(BOOL success) {
//        XMBindPhoneViewController *bindPhoneVC = [[XMBindPhoneViewController alloc] init];
//        [weakSelf.navigationController pushViewController:bindPhoneVC animated:YES];
//    }];
//}

- (void)loginWithWechat {
    [WXApi registerApp:@"wxe27fd4207b87851f" universalLink:@"https://help.wechat.com/zmy/"];
    SendAuthReq *req = [[SendAuthReq alloc] init];
    req.scope = @"snsapi_userinfo";
    req.state = @"com.xmjj_zmy";
    [WXApi sendReq:req completion:^(BOOL success) {
        
    }];
}

- (void)onResp:(BaseResp *)resp {
    if (!resp.errStr || resp.errStr.length == 0) {
        [self getWeichatInfoWithRespcode:((SendAuthResp *)resp).code];
    }
}

//获取微信登录信息
- (void)getWeichatInfoWithRespcode:(NSString *)respcode {
    MJWeakSelf;
    [_Network getWechatInfoWithRespcode:respcode
                                success:^(id  _Nonnull responseObject) {
//        NSString *errerrcode = [responseObject safeStringForKey:@"errcode"];
        NSString *unionidStr = [responseObject safeStringForKey:@"unionid"];
        NSString *openId = [responseObject safeStringForKey:@"openid"];
        if (unionidStr && unionidStr.length > 0) {
            XMBindPhoneViewController *bindPhoneVC = [[XMBindPhoneViewController alloc] init];
            bindPhoneVC.unionidStr = unionidStr;
            bindPhoneVC.openId = openId;
            [weakSelf.navigationController pushViewController:bindPhoneVC animated:YES];
//            [weakSelf loginWithWeichatUnionid:unionidStr];
        }
        else {
            [MBProgressHUD showError:@"登录失败"];
        }
//        if (!errerrcode || errerrcode.length == 0) {
//            [weakSelf loginWithWeichatUnionid:];
//        }
//        else {
//            [MBProgressHUD showError:[responseObject safeStringForKey:@"errmsg"]];
//        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

//微信登录
- (void)loginWithWeichatUnionid:(NSString *)unionid {
    MJWeakSelf
    [_Network wechatLoginWithCode:unionid
                          success:^(id  _Nonnull responseObject) {
        [MBProgressHUD showSuccess:@"登录成功"];
        XMUserModel *userModel = [XMUserModel yy_modelWithJSON:responseObject[@"data"]];
        [[XMUserManager shareInstance] saveUserModel:userModel];
        XMBindPhoneViewController *bindPhoneVC = [[XMBindPhoneViewController alloc] init];
        [weakSelf.navigationController pushViewController:bindPhoneVC animated:YES];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

//服务条款
- (void)checkService {
    XMServiceInfoViewController *serviceInfoVC = [[XMServiceInfoViewController alloc] init];
    serviceInfoVC.type = @"4";
    serviceInfoVC.titleStr = @"服务条款";
    [self.navigationController pushViewController:serviceInfoVC animated:YES];
}

//隐私设置
- (void)checkPrivacy {
    XMServiceInfoViewController *serviceInfoVC = [[XMServiceInfoViewController alloc] init];
    serviceInfoVC.type = @"5";
    serviceInfoVC.titleStr = @"隐私政策";
    [self.navigationController pushViewController:serviceInfoVC animated:YES];
}

#pragma mark - Init
- (TencentOAuth *)tencentOAth {
    if (!_tencentOAth) {
        _tencentOAth = [[TencentOAuth alloc] initWithAppId:@"1112444128" andDelegate:self];
    }
    return _tencentOAth;
}

//- (void)lookSelectItemAtIndex {
//    XMHomeViewController * homgVc = [[XMHomeViewController alloc]init];
//    [self.navigationController  pushViewController:homgVc animated:YES];
//}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
